﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


//保存物品栏
[Serializable]
public class SlotData
{
    public ItemData item;
    public int count = 0;

    private Action OnChange;

    //识别当前格子是否有格外的空间
    public bool CanAddItem()
    {
        return count < item.maxCount;
    }
    //剩余多少个空间可以添加
    public int GetFreeSpace()
    {
        return item.maxCount - count;
    }
    //判断当前格式是否为空
    public bool IsEmpty()
    {
        return count == 0;
    }
    //增加物品数量
    public void Add(int numToAdd = 1)
    {
        this.count += numToAdd;
        OnChange?.Invoke();
    }
    //增加物品
    public void AddItem(ItemData item, int count = 1)
    {
        this.item = item;
        this.count = count;
        OnChange?.Invoke();
    }
    //更新列表
    public void AddListener(Action OnChange)
    {
        this.OnChange = OnChange;
    }
    //清空
    public void Clear()
    {
        item = null;
        count = 0;
        OnChange?.Invoke();
    }
    //清空手上丢弃的物品,默认减少一个
    public void Reduce(int numToRedue = 1)
    {
        count -= numToRedue;
        if (count == 0)
        {
            Clear();
        }
        else
        {
            OnChange?.Invoke();
        }
    }
    //移动物品
    public void MoveSlot(SlotData data)
    {
        this.item = data.item;
        this.count = data.count;
        OnChange?.Invoke();
    }
}


